package com.ruoyi.api.supply;

import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.DictUtils;
import com.ruoyi.common.utils.IOUtil;
import com.ruoyi.common.utils.StrKit;
import com.ruoyi.system.domain.CompanySupply;
import com.ruoyi.system.domain.ListDetail;
import com.ruoyi.system.domain.OrderList;
import com.ruoyi.system.domain.Supplier;
import com.ruoyi.system.mapper.ListDetailMapper;
import com.ruoyi.system.service.ICompanySupplyService;
import com.ruoyi.system.service.IOrderListService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 订单相关
 */
@RequestMapping("/api/supply/orderList")
@Controller
public class OrderListCtrl extends SupplyBaseCtrl{

    @Autowired
    IOrderListService orderListService;
    @Autowired
    ICompanySupplyService companySupplyService;

    //==================商家角色功能开始=
    /**
     * 查询待验收的订单——商店角色使用
     * @return
     */
    @RequestMapping("/getShopCheckList")
    @ResponseBody
    public TableDataInfo getShopCheckList(ListDetail detail)
    {
        startPage();
        SysUser user = getLoginShop();
        Long shopId=user.getShopId();
        //查找带录入的商品
        if(detail == null){
            detail = new ListDetail();
        }
        detail.setStatus("1");
        Map<String,Object> map=new HashMap<>();
        map.put("statusin","('3','31')");//只显示待验收的商品
        map.put("shopId",shopId);
        detail.setParams(map);
        List<ListDetail> list= orderListService.getListGoods(detail);
        TableDataInfo tableDataInfo=getDataTable(list);
        return tableDataInfo;
    }

    /**
     * 商家验收订单——商家角色使用
     * @param data
     * @return
     */
    @RequestMapping("/shopCheckList")
    @ResponseBody
    public AjaxResult shopCheckList(@RequestBody JSONObject data)
    {
        //data = {"data":[{"id":1,"refundNum":"1.2","checkRemark":"有不好的"}]};
        List<ListDetail> details = new ArrayList<>();
        SysUser user = getLoginShop();
        JSONArray jsa = data.getJSONArray("data");
        for (int i = 0; i < jsa.size(); i++) {
            JSONObject jo = jsa.getJSONObject(i);
            ListDetail detail = new ListDetail();
            detail.setId(jo.getLong("id"));
            detail.setRefundNum(jo.getDouble("refundNum"));
            detail.setCheckRemark(jo.getString("checkRemark"));
            detail.setStatus("2");
            detail.setCheckUser(user.getNickName());
            details.add(detail);
        }

        //核验订单
        AjaxResult result = orderListService.checkOrder(details,user);
        return result;
    }

    //==================商家角色功能结束

    /**
     * 查询我的订单列表——正在进行中
     * @param orderList
     */
    @RequestMapping("/getMyOrderListGoing")
    @ResponseBody
    public TableDataInfo getMyOrderListGoing(OrderList orderList, @RequestParam(name="beginTime",required = false)String beginTime, @RequestParam(name="endTime",required = false)String endTime)
    {
        startPage();
        if(orderList==null){
            orderList=new OrderList();
        }
        if(StrKit.notBlank(beginTime)){
            orderList.getParams().put("beginCtime",beginTime);
        }
        if(StrKit.notBlank(endTime)){
            orderList.getParams().put("endCtime",endTime);
        }
        //设置自己账号
        Supplier supplier=getLoginSupplier();
        orderList.setSupplyId(supplier.getId());
        orderList.setStatusin("('1','2','3','31')");
        List<OrderList> list=orderListService.selectOrderListList(orderList);
        return getDataTable(list);
    }

    /**
     * 查询我的订单列表——已完成
     * @param orderList
     */
    @RequestMapping("/getMyOrderListFinish")
    @ResponseBody
    public TableDataInfo getMyOrderListFinish(OrderList orderList, @RequestParam(name="beginTime",required = false)String beginTime, @RequestParam(name="endTime",required = false)String endTime)
    {
        startPage();
        if(orderList==null){
            orderList=new OrderList();
        }
        if(StrKit.notBlank(beginTime)){
            orderList.getParams().put("beginCtime",beginTime);
        }
        if(StrKit.notBlank(endTime)){
            orderList.getParams().put("endCtime",endTime);
        }
        //设置自己账号
        Supplier supplier=getLoginSupplier();
        orderList.setSupplyId(supplier.getId());
        orderList.setStatusin("('4','5')");
        List<OrderList> list=orderListService.selectOrderListList(orderList);
        return getDataTable(list);
    }

    /**
     * 查询我的订单列表
     * @param orderList
     */
    @RequestMapping("/getMyOrderList")
    @ResponseBody
    public TableDataInfo getMyOrderList(OrderList orderList)
    {
        startPage();
        if(orderList==null){
            orderList=new OrderList();
        }
        //设置自己账号
        Supplier supplier=getLoginSupplier();
        orderList.setSupplyId(supplier.getId());
        List<OrderList> list=orderListService.selectOrderListList(orderList);
        return getDataTable(list);
    }


    /**
     * 查询订单商品详情
     * @param id
     */
    @RequestMapping("/getListGoods")
    @ResponseBody
    public TableDataInfo getListGoods(Long id) {

        List<ListDetail> list = orderListService.getListGoods(id);

        return getDataTable(list);
    }

    /**
     * 查询订单详情
     */
    @RequestMapping("/getOrderListById")
    @ResponseBody
    public AjaxResult getOrderListById(Long id) {
        OrderList orderList = orderListService.selectOrderListById(id);
        return AjaxResult.success(orderList);
    }

    /**
     * 修改订单商品信息
     * @param jsonObject
     * @return
     */
    @RequestMapping("/takeOrder")
    @ResponseBody
    public AjaxResult takeOrder(@RequestBody JSONObject jsonObject) {
        AjaxResult result = orderListService.takeOrder(jsonObject);

        return result;
    }

    /**
     * 查询我需要提供的商品——采购统计
     * @param detail
     * @return
     */
    @RequestMapping("/getWantedGoods")
    @ResponseBody
    public TableDataInfo getWantedGoods(ListDetail detail)
    {
        startPage();
        if(detail==null){detail=new ListDetail();}
        Supplier supplier=getLoginSupplier();
        Map<String,Object> map=new HashMap<>();
        map.put("statusin","('0','1','2','3')");
        map.put("supplyId",supplier.getId());
        detail.setParams(map);
        List<ListDetail> list= orderListService.getListGoods(detail);
        return getDataTable(list);
    }

}
